package com.ds.common.route.config;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.ds.common.nacos.ConfigModel;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.Map;

@Data
public class AgentConfig implements ConfigModel {

    Integer agentId;

    String cdnUrl;

    String bucketName;

    Map<String, FrontConfig> frontConfigMap = Maps.newHashMap();

    CorporationConfig corporationConfig;

    public FrontConfig getFrontConfig(Integer agentId, String productName) {

        FrontConfig productConfig = frontConfigMap.get(productName);
        FrontConfig result = new FrontConfig();

        JSONObject variables = new JSONObject();

        if (productConfig != null) {
            result.setProductName(productConfig.getProductName());
            result.setVersion(productConfig.getVersion());
            variables.putAll(productConfig.getVariables());
        }
        result.setVariables(variables);
        return result;
    }

    @Override
    public boolean isValid() {
        //成员不能为空
        return agentId != null && !CollectionUtils.isEmpty(frontConfigMap) && corporationConfig != null && cdnUrl != null && bucketName != null;
    }
}
